package com.yanchangapp.ui.presenter;

import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;
import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.bean.ComposeMenuIcons;
import com.yanchangapp.model.bean.MenuIcons;
import com.yanchangapp.model.response.MenuResponse;
import com.yanchangapp.ui.fragment.home.HomeFragment;
import com.yanchangapp.ui.view.WorkFgView;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class WorkFgPresenter extends BasePresenter<WorkFgView> {

    public WorkFgPresenter(BaseActivity context) {
        super(context);
    }

    public void fetchList(){
        List<MenuResponse.Bean> mStaticMenuList = HomeFragment.mStaticMenuList;
        // 重新进行请求
        if (mStaticMenuList == null || mStaticMenuList.size() == 0){
            queryMenuLists();
        }else {
            parseList(mStaticMenuList);
        }
    }

    private void queryMenuLists(){
        HeadApiRetrofit.getInstance().queryMenuLists()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<MenuResponse>() {
                    @Override
                    public void onNext(MenuResponse menuResponse) {
                        if (menuResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            HomeFragment.mStaticMenuList = menuResponse.getList();
                            parseList(menuResponse.getList());
                        }else {
                            if (getView() != null){
                                getView().queryWorkListFailure(menuResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().queryWorkListFailure("获取工作列表失败");
                        }
                    }
                });
    }

    // 进行数据解析
    private void parseList(List<MenuResponse.Bean> list){
        List<ComposeMenuIcons> mTargetList = new ArrayList<>();
        if (list == null || list.size() == 0){
            getView().queryWorkListSuccess(mTargetList);
            return;
        }
        for (int i=0;i<list.size();i++){
            MenuResponse.Bean firstBean = list.get(i);
            List<MenuIcons> menuIcons = new ArrayList<>();
            menuIcons.clear();
            if (firstBean.getName().equals("邮件")){
                List<MenuResponse.Bean> firstBeanList = firstBean.getList();
                if (firstBeanList != null){
                    for (int j=0;j<firstBeanList.size();j++){
                        MenuResponse.Bean bean = firstBeanList.get(i);
                        menuIcons.add(new MenuIcons(bean.getId(),bean.getName(),bean.getImageAddress(),bean.getMobileAddress()));
                    }
                }
            }else {
                List<MenuResponse.Bean> secondBeanList = firstBean.getList();
                if (secondBeanList != null){
                    for (int j=0;j<secondBeanList.size();j++){
                        List<MenuResponse.Bean> thirdLists = secondBeanList.get(j).getList();
                        for (int k=0;k<thirdLists.size();k++){
                            MenuResponse.Bean bean = thirdLists.get(k);
                            menuIcons.add(new MenuIcons(bean.getId(),bean.getName(),bean.getImageAddress(),bean.getMobileAddress()));
                        }
                    }
                }
            }
            mTargetList.add(new ComposeMenuIcons(firstBean.getName(),menuIcons));
        }
        if (getView() != null){
            getView().queryWorkListSuccess(mTargetList);
        }
    }
}
